<template>
  <div>
    <el-tree
      ref="tree"
      :data="list"
      :props="{label: 'name'}"
      default-expand-all
      show-checkbox
      check-strictly
      node-key="id"
    />
    <div class="footer">
      <el-button @click="$emit('close')">取消</el-button>
      <el-button type="primary" @click="hSubmit">确认</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permissions'
import { assignPermission, getPermissionDetail } from '@/api/settings'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'Hr75TwiceAssignpermission',
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermission()
    this.PermissionDetail()
  },

  methods: {
    async loadPermission() {
      try {
        const res = await getPermissionList()
        // console.log(res)
        this.list = tranListToTreeData(res.data)
      } catch (e) {
        console.log(e)
      }
    },
    async PermissionDetail() {
      try {
        const res = await getPermissionDetail(this.id)
        // console.log(res)
        // 数据回填 需要打牌el-tree里 node-key 属性使用
        this.$refs.tree.setCheckedKeys(res.data.permIds)
      } catch (e) {
        console.log(e)
      }
    },
    async hSubmit() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys()
        // console.log(permIds)
        const res = await assignPermission({ id: this.id, permIds })
        // console.log(res)
        this.$message.success(res.message)
        this.$emit('close')
      } catch (e) {
        this.$message.error(e.message)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.footer{
  display: flex;
  justify-content: end;
}
</style>
